package org.example.aop_demo1.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Component
@Aspect
public class LogAop {

    @Pointcut("execution(* org.example.aop_demo1.controller.*.*(..))")
    public void controllerPointcut() {}

    @Pointcut("@annotation(org.example.aop_demo1.annotation.Log)")
    public void logAnnotation(){}



    @Before("controllerPointcut() ")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName());
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        System.out.println(request.getRequestURL());
        System.out.println(Arrays.toString(joinPoint.getArgs()));

    }

    @Around("logAnnotation()")
    public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start logAround");
        System.out.println(joinPoint.getSignature().getName());
        System.out.println(Arrays.toString(joinPoint.getArgs()));
        Object proceed = joinPoint.proceed();
        System.out.println("around logAround返回数据:");
        System.out.println(proceed);
        System.out.println("end logAround");
//        System.out.println(joinPoint.get);
    }

    @After("logAnnotation()")
    public void logAfter(JoinPoint joinPoint) {

    }
    @AfterReturning(value = "logAnnotation()",returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        System.out.println("end logAfterReturning");
        System.out.println(result);
    }
    @AfterThrowing(value = "logAnnotation()",throwing = "ex")
    public void logAfterThrowing(JoinPoint joinPoint, Exception ex) {

    }
}
